#!/usr/bin/env bash
# Copyright (c) 2024 The Toltec Contributors
# SPDX-License-Identifier: MIT

archs=(rmallos2)
pkgnames=(innernet-client)
pkgdesc="A private network system that uses WireGuard under the hood."
url="https://github.com/tonarino/innernet"
pkgver=1.6.1-1
timestamp=2024-01-19T14:24:40Z
section="utils"
maintainer="plan5 <30434574+plan5@users.noreply.github.com>"
license=MIT
installdepends=(wireguard)
makedepends=(build:librust-clang-sys-dev build:libclang-dev build:libc6 build:libc6-dev build:clang)

image=rust:v3.1
_srcver="v${pkgver%-*}"
source=(
    "https://github.com/tonarino/innernet/archive/refs/tags/$_srcver.zip"
)
sha256sums=(
    fd9ec6eb75dcc46c818696e7fcb87eb772dc024e0c326360f0f8c473c937c37c
)

prepare() {
    # Change config folders to live under /opt
    sed -i "s/\/etc\/innernet/\/opt\/etc\/innernet/g" "$srcdir/shared/src/lib.rs"
    sed -i "s/\/var\/lib\/innernet/\/opt\/var\/lib\/innernet/g" "$srcdir/shared/src/lib.rs"

    # Change binary path in service file
    sed -i "s/\/usr\/bin\/innernet/\/opt\/bin\/innernet/g" "$srcdir/client/innernet@.service"

    #tbd: change wireguard config dir to /opt/etc/wireguard?
    #sed -i "s/\/etc\/wireguard/\/opt\/etc\/wireguard/g" "$srcdir/shared/src/lib.rs"
}

build() {
    cargo build --release --bin innernet
}

package() {
    install -D -m 700 -t "$pkgdir"/opt/bin "$srcdir"/target/armv7-unknown-linux-gnueabihf/release/innernet
    install -D -m 644 -t "$pkgdir"/lib/systemd/system "$srcdir/client/innernet@.service"
    install -D -m 644 -t "$pkgdir"/lib/systemd/system "$srcdir/client/innernet.target"
}

configure() {
    if ! [ -d /opt/var/lib/innernet ]; then
        mkdir -p /opt/var/lib/innernet
    fi
}

preremove() {
    disable-unit innernet.target
    disable-unit innernet@.service
}

postremove() {
    systemctl daemon-reload
}
